﻿package mortal.game.view.copy.ChallengeCopy.view
{
    import Message.DB.Tables.*;
    import Message.Public.*;
    import __AS3__.vec.*;
    import com.greensock.*;
    import com.greensock.easing.*;
    import com.mui.controls.*;
    import com.mui.utils.*;
    import extend.language.*;
    import flash.events.*;
    import flash.geom.*;
    import flash.text.*;
    import mortal.common.*;
    import mortal.common.global.*;
    import mortal.common.swfPlayer.*;
    import mortal.common.swfPlayer.data.*;
    import mortal.component.gconst.*;
    import mortal.component.window.*;
    import mortal.game.events.*;
    import mortal.game.mvc.*;
    import mortal.game.resource.*;
    import mortal.game.resource.info.item.*;
    import mortal.game.resource.tableConfig.*;
    import mortal.game.utils.*;
    import mortal.game.view.common.*;
    import mortal.game.view.common.display.*;
    import mortal.game.view.common.item.*;
    import mortal.game.view.copy.ChallengeCopy.newView.*;
    import mortal.game.view.medal.*;
    import mortal.mvc.core.*;
    import mortal.mvc.interfaces.*;

    public class ChallengePassWin extends ResultBaseWindow
    {
        private var _towerName:GImageBitmap;
        private var _levelName:GImageBitmap;
        private var _tipsBitmap:GImageBitmap;
        private var _nextSection:GTextFiled;
        private var _nextLevel:GTextFiled;
        private var _midItemSprite:GSprite;
        private var _leftTime:SecTimerView;
        protected var _swfPlayer:SWFPlayer;
        private var _reward:SChallengeCopyRewards;
        private var _medalLevel:BitmapNumberText;
        private var _tween:TweenMax;
        private var _rewardList:Vector.<BaseItem>;
        private static var _instance:ChallengePassWin;

        public function ChallengePassWin(param1:ILayer = null)
        {
            this._rewardList = new Vector.<BaseItem>;
            if (_instance != null)
            {
                throw new Error(" ChallengPassWin 单例 ");
            }
            return;
        }// end function

        override protected function createDisposedChildrenImpl() : void
        {
            super.createDisposedChildrenImpl();
            this.pushUIToDisposeVec(UIFactory.bg(155, 245, 432, 2, this, ImagesConst.SplitLine));
            this.pushUIToDisposeVec(UIFactory.gImageBitmap(ImagesConst.challengeCopyNewTxt_5, 248, 194, this));
            var _loc_1:* = UIFactory.gBitmap(ImagesConst.GroupCopySide, 205, 51, this);
            var _loc_2:* = UIFactory.gBitmap(ImagesConst.GroupCopySide, 495, 51, this);
            _loc_2.scaleX = -1;
            this.pushUIToDisposeVec(_loc_1);
            this.pushUIToDisposeVec(_loc_2);
            this._tipsBitmap = UIFactory.gImageBitmap(ImagesConst.challengeCopyNewTxt_6, 173, 220, this);
            this._towerName = UIFactory.gImageBitmap("", 145, 80, this);
            this._levelName = UIFactory.gImageBitmap("", 0, 45, this, this.updateNamePos);
            var _loc_3:* = GlobalStyle.textFormatHuang2.center();
            _loc_3.size = 15;
            this._nextSection = UIFactory.gTextField("", 246, 220, 100, 30, this, _loc_3);
            this._nextLevel = UIFactory.gTextField("", 373, 220, 100, 30, this, _loc_3);
            _loc_3 = _loc_3.left();
            this._leftTime = UIFactory.secTimeView(Language.getString(35132), 420, 253, 60, 20, this, _loc_3);
            this._leftTime.filters = [FilterConst.glowFilter];
            this._leftTime.autoSize = TextFieldAutoSize.LEFT;
            this._leftTime.mouseEnabled = false;
            this._leftTime.configEventListener(EventName.SecViewTimeChange, this.onSecViewTimeChangeHandler);
            this._leftTime.setLeftTime(10, true);
            setContentBgSize(370, 128);
            return;
        }// end function

        override protected function disposeImpl(param1:Boolean = true) : void
        {
            this._towerName.dispose(param1);
            this._towerName = null;
            this._levelName.dispose(param1);
            this._levelName = null;
            this._nextSection.dispose(param1);
            this._nextSection = null;
            this._nextLevel.dispose(param1);
            this._nextLevel = null;
            this._tipsBitmap.dispose(param1);
            this._tipsBitmap = null;
            this._leftTime.dispose(param1);
            this._leftTime = null;
            if (this._swfPlayer)
            {
                this._swfPlayer.dispose(param1);
                this._swfPlayer = null;
            }
            if (this._medalLevel)
            {
                this._medalLevel.dispose(param1);
                this._medalLevel = null;
            }
            if (this._tween)
            {
                this._tween.kill();
                this._tween = null;
            }
            if (this._midItemSprite)
            {
                this._midItemSprite.dispose(param1);
                this._midItemSprite = null;
            }
            this.removeRewardItems();
            this._reward = null;
            super.disposeImpl(param1);
            return;
        }// end function

        private function addMedalItem() : void
        {
            this._midItemSprite = UICompomentPool.getUICompoment(GSprite);
            this.addChild(this._midItemSprite);
            this.pushUIToDisposeVec(UIFactory.gImageBitmap(ImagesConst.challengeCopyNewTxt_12, 15, 75, this._midItemSprite));
            this.pushUIToDisposeVec(UIFactory.gImageBitmap(ImagesConst.medal_overSkin, 293, 80, this));
            var _loc_1:* = UIFactory.gImageBitmap(ImagesConst.ChallengePassWinLine, 280, 85, this);
            this.pushUIToDisposeVec(_loc_1);
            _loc_1 = UIFactory.gImageBitmap(ImagesConst.ChallengePassWinLine, 430, 85, this);
            _loc_1.scaleX = -1;
            this.pushUIToDisposeVec(_loc_1);
            this._medalLevel = UIFactory.bitmapNumberText(40, 78, "FightInfoNum.png", 60, 100, -1, this._midItemSprite);
            this._swfPlayer = UIFactory.getUICompoment(SWFPlayer);
            this._swfPlayer.timeRate = 3;
            this._swfPlayer.move(353 - 317, 133 - 95);
            this._swfPlayer.load("ActionIconEffect.swf", ModelType.NormalSwf, null);
            return;
        }// end function

        override protected function updateSize() : void
        {
            super.updateSize();
            if (_enterBtn)
            {
                _enterBtn.y = this.height - _enterBtn.height - 84 + 10;
            }
            return;
        }// end function

        private function updateNamePos() : void
        {
            if (isDisposed)
            {
                return;
            }
            this._levelName.x = (this.width - this._levelName.width) * 0.5 + 1;
            return;
        }// end function

        private function updateRewardList() : void
        {
            var _loc_1:BaseItem = null;
            var _loc_4:Array = null;
            var _loc_5:ItemData = null;
            var _loc_6:int = 0;
            var _loc_8:int = 0;
            var _loc_9:int = 0;
            var _loc_10:int = 0;
            var _loc_11:int = 0;
            var _loc_12:int = 0;
            var _loc_13:int = 0;
            this.removeRewardItems();
            var _loc_2:* = ChallengeCopyConfig.instance.getDataByTowerAndSectionAndDif(this._reward.result.floor, this._reward.result.level, this._reward.result.diff);
            var _loc_3:* = _loc_2.reward.split("&");
            while (_loc_6 < _loc_3.length)
            {
                
                if (_loc_3[_loc_6])
                {
                    _loc_4 = (_loc_3[_loc_6] as String).split("#");
                    _loc_5 = new ItemData(int(_loc_4[0]));
                    if (_loc_5.itemInfo && ItemsUtil.isFitCarrer(_loc_5))
                    {
                        _loc_1 = UICompomentPool.getUICompoment(BaseItem);
                        _loc_1.setItemStyle(ItemStyleConst.Big, ImagesConst.PackItemBg, 2, 2);
                        _loc_1.itemData = _loc_5;
                        _loc_1.amount = _loc_4[1];
                        _loc_1.filters = this._reward.result.isPass ? ([]) : ([FilterConst.colorFilter2]);
                        this.addChild(_loc_1);
                        this._rewardList.push(_loc_1);
                    }
                }
                _loc_6++;
            }
            var _loc_7:* = MedalConfig.instance.getTmedalByCode(_loc_2.medalId);
            if (_loc_7)
            {
                this.addMedalItem();
                _loc_6 = 0;
                while (_loc_6 < this._rewardList.length)
                {
                    
                    this._rewardList[_loc_6].x = 220 * _loc_6 + 210;
                    this._rewardList[_loc_6].y = 104;
                    _loc_6++;
                }
                this.addChild(this._midItemSprite);
                _loc_1 = UICompomentPool.getUICompoment(BaseItem);
                _loc_1.setSize(66, 68);
                _loc_1.isShowLock = false;
                _loc_1.source = MedalUtil.getMedalIconUrlByTmedal(_loc_7, 1);
                _loc_1.toolTipData = _loc_7;
                this._midItemSprite.addChild(_loc_1);
                this._midItemSprite.x = 350;
                this._midItemSprite.y = 135;
                var _loc_14:Number = 0.2;
                this._midItemSprite.scaleY = 0.2;
                this._midItemSprite.scaleX = _loc_14;
                this._rewardList.push(_loc_1);
                this._tween = TweenMax.to(this._midItemSprite, 0.4, {x:317, y:95, alpha:1, scaleX:1, scaleY:1, ease:Back.easeOut, onComplete:this.showSwf});
            }
            else
            {
                _loc_8 = this._rewardList.length;
                _loc_9 = 100;
                _loc_10 = 5;
                _loc_6 = 0;
                while (_loc_6 < _loc_8)
                {
                    
                    _loc_13 = int(_loc_6 / _loc_10);
                    _loc_11 = _loc_8 - _loc_13 * _loc_10 >= _loc_10 ? (_loc_10) : (_loc_8 - _loc_13 * _loc_10);
                    _loc_12 = (this.width - _loc_11 * 80 + 3) * 0.5;
                    this._rewardList[_loc_6].x = _loc_12 + _loc_9 * (_loc_6 % _loc_10);
                    this._rewardList[_loc_6].y = 104 + _loc_13 * _loc_9;
                    this._rewardList[_loc_6].showEffect(true, 36, 32, "ArriundEffect_cheng_big.swf");
                    _loc_6++;
                }
            }
            return;
        }// end function

        private function showSwf() : void
        {
            this._midItemSprite.addChild(this._swfPlayer);
            return;
        }// end function

        private function removeRewardItems() : void
        {
            var _loc_1:BaseItem = null;
            for each (_loc_1 in this._rewardList)
            {
                
                _loc_1.dispose(true);
            }
            this._rewardList.length = 0;
            return;
        }// end function

        override protected function enterBtnClickHandler(event:MouseEvent) : void
        {
            this.leaveCopy();
            return;
        }// end function

        private function leaveCopy() : void
        {
            if (this._reward.result.isPass)
            {
                this.flyIcon();
            }
            hide();
            Dispatcher.dispatchEvent(new DataEvent(EventName.CopyLeave));
            return;
        }// end function

        private function flyIcon() : void
        {
            var _loc_1:BaseItem = null;
            var _loc_2:Point = null;
            for each (_loc_1 in this._rewardList)
            {
                
                if (_loc_1.itemData)
                {
                    _loc_2 = _loc_1.localToGlobal(new Point(0, 0));
                    FlyToNavbarTool.flyToBackPack(_loc_1.bitmapdata, _loc_2);
                }
            }
            return;
        }// end function

        private function updateDifficultIcon() : void
        {
            if (isDisposed)
            {
                return;
            }
            return;
        }// end function

        public function set reward(param1:SChallengeCopyRewards) : void
        {
            this._reward = param1;
            if (this._reward.result.isPass)
            {
                resultType = showSuccess;
            }
            else
            {
                resultType = showFail;
            }
            this.updateRewardList();
            this._towerName.imgUrl = "ExpeditionTowerName_" + param1.result.floor + "T.png";
            this._levelName.imgUrl = "ExpeditionName_" + ChallengeUtil.getLevelUrlByTowerSectionAndLevel(param1.result.floor, param1.result.level, param1.result.diff);
            var _loc_2:* = ChallengeCopyConfig.instance.getDataByTowerAndSectionAndDif(this._reward.result.floor, this._reward.result.level, this._reward.result.diff);
            var _loc_3:* = MedalConfig.instance.getTmedalByCode(_loc_2.medalId);
            if (_loc_3)
            {
                this._medalLevel.text = "" + _loc_3.typeLevel;
            }
            var _loc_4:* = ChallengeCopyConfig.instance.getDataByTowerAndSectionAndDif(param1.result.floor, param1.result.level, (param1.result.diff + 1));
            if (_loc_4 == null)
            {
                _loc_4 = ChallengeCopyConfig.instance.getDataByTowerAndSectionAndDif(param1.result.floor, (param1.result.level + 1), 1);
            }
            if (_loc_4)
            {
                this._nextSection.text = _loc_4.sectionName;
                this._nextLevel.text = _loc_4.levelName;
                this._tipsBitmap.visible = true;
                this._nextSection.x = 246;
            }
            else
            {
                this._nextSection.text = Language.getString(35133);
                this._nextLevel.text = "";
                this._tipsBitmap.visible = false;
                this._nextSection.x = 246 + 56;
            }
            return;
        }// end function

        private function onSecViewTimeChangeHandler(event:DataEvent) : void
        {
            var _loc_2:* = event.data as int;
            if (_loc_2 <= 0)
            {
                if (this._leftTime.parent)
                {
                    this._leftTime.parent.removeChild(this._leftTime);
                }
                this.leaveCopy();
            }
            return;
        }// end function

        public function get reward() : SChallengeCopyRewards
        {
            return this._reward;
        }// end function

        public static function get instance() : ChallengePassWin
        {
            if (_instance == null)
            {
                _instance = new ChallengePassWin;
            }
            return _instance;
        }// end function

        public static function get isViewShow() : Boolean
        {
            return _instance && !_instance.isHide;
        }// end function

    }
}
